package com.dtda.edu.miaosha.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.dtda.edu.miaosha.bo.ItemBo;
import com.dtda.edu.miaosha.common.exception.BaseExcpController;
import com.dtda.edu.miaosha.common.exception.BusinessException;
import com.dtda.edu.miaosha.common.response.ResponseType;
import com.dtda.edu.miaosha.service.ItemService;

/**
 * @author 袁强
 * @version 2019年3月17日 下午9:15:16
 * 类说明  
 */
@RestController
@RequestMapping("/")
public class ManageItemController  extends BaseExcpController{

	@Autowired
	private ItemService itemService;
	
	@RequestMapping("/create")
	@ResponseBody
	public Map<String, String> createItem(@RequestParam(name="title")String title,
								   @RequestParam(name="price")BigDecimal price,
								   @RequestParam(name="description")String description,
								   @RequestParam(name="imgUrl")String imgUrl,
								   @RequestParam(name="titleStock")String titleStock) throws BusinessException {
		
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("code", "fail");  //默认返回失败
		map.put("message", "添加失败");
		
		//@RequestParam(name="sales")Integer sales  銷售和創建商品無關
		ItemBo itemBo = new ItemBo();
		itemBo.setTitle(title);
		itemBo.setPrice(price);
		itemBo.setImgUrl(imgUrl);
		itemBo.setDescription(description);
		itemBo.setStock(Integer.valueOf(titleStock));
		itemBo.setSales(0);   //初始銷售為0
		ItemBo createTitle = itemService.createTitle(itemBo);
		if(createTitle == null) {
			return map;
		}
		map.put("code", "success");
		map.put("message", "添加成功");
		return map;
	}
	@RequestMapping("/createItem")
	public ModelAndView createItem() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/project/createItem");
		return modelAndView;
	}
	@RequestMapping("/showItems")
	@ResponseBody
	public ResponseType showItems(){
		List<ItemBo> items = itemService.showItems();
		if(items.size() == 0) {
			return ResponseType.create("fail", "没有活动商品!");
		}
		return ResponseType.create(items);
	}
	@RequestMapping("/selAll")
	public ModelAndView selAll() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/project/showItems");
		return modelAndView;
	}
	@RequestMapping("/updateItem")
	@ResponseBody
	public ModelAndView updateItem(@RequestParam(name="id")String id){
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("itemId", id);
		modelAndView.setViewName("/project/ItemDetail");
		return modelAndView;
	}
	@RequestMapping("/updItem")
	@ResponseBody
	public ResponseType updItem(
			@RequestParam(name="itemId")String itemId,
			   @RequestParam(name="title")String title,
			   @RequestParam(name="imgUrl")String imgUrl,
			   @RequestParam(name="price")BigDecimal price,
			   @RequestParam(name="description")String description,
			   @RequestParam(name="titleStock")String titleStock,
			   @RequestParam(name="titleSales")String titleSales) {
		ItemBo itemBo = new ItemBo();
		itemBo.setId(Integer.valueOf(itemId));
		itemBo.setTitle(title);
		itemBo.setDescription(description);
		itemBo.setImgUrl(imgUrl);
		itemBo.setPrice(price);
		itemBo.setSales(Integer.valueOf(titleSales));
		itemBo.setStock(Integer.valueOf(titleStock));
		boolean b= itemService.updateItem(itemBo);
		if(! b) {
			return ResponseType.create("fail", "修改商品失败!");
		}
		return ResponseType.create("修改商品成功!");
	}
	@RequestMapping("/delItem")
	@ResponseBody
	public ResponseType delItem(@RequestParam(name="id")String id) {
		boolean b = itemService.deleteItem(Integer.valueOf(id));
		if(! b) {
			return ResponseType.create("fail", "删除商品失败!");
		}
		return ResponseType.create("删除商品成功!");
	}
	@RequestMapping("/selItem")
	@ResponseBody
	public ResponseType getItem(@RequestParam(name="id")Integer id){
		ItemBo itemBo = itemService.showItemById(id);
		if(itemBo == null) {
			return ResponseType.create("fail", "商品不存在!");
		}
		return ResponseType.create(itemBo);
	}
}
